var arr = ['tproxyInfo.html', 'linkInfo.html', 'linkBase.html', 'localdns.html'];
leftNaviFunc(arr);
$(function() {
    $.ajax({
        url: "http://" + apihost + "/v1/tproxy/",
        type: 'get',
        dataType: 'json',
        success: function(data) {
            var dateHtml = "";
            if (data.retcode == 1) {
                window.location.replace("/login.html")
                return
            }
            if (data.retcode != 0) {
                alert(data.desc)
                return
            }
            for (var i = 0; i < data.value.length; i++) {
                var isTure, isuse, anthorText;
                (data.value[i].Enable == 0) ? isTrue = "否": isTrue = "是";
                (data.value[i].Unavailable == 0) ? isuse = "可用": isuse = "不可用";
                (data.value[i].Remark == "") ? anthorText = "无": anthorText = data.value[i].Remark;
                dateHtml += '<div class="listContent_item">';
                dateHtml += '<div class="change"><span>' + data.value[i].Id + '</span></div>';
                dateHtml += '<div class="change"><span>' + data.value[i].Name + '</span></div>';
                dateHtml += '<div class="change"><span>' + data.value[i].Addr + '</span></div>';
                dateHtml += '<div class="change"><span>' + data.value[i].Capacity + '</span></div>'
                dateHtml += '<div class="change"><span>' + isTrue + '</span></div>';
                dateHtml += '<div class="change"><span>' + isuse + '</span></div>';
                dateHtml += '<div class="second change"><span>' + anthorText + '</span></div>';
                dateHtml += '<div><span class="amend">修改 </span><span> / <span class="delete"> 删除</span></span></div><div class="clear"></div></div>';
            }
            $(".listContent").html(dateHtml);
        },
        error: function(a, b, c) {
            alert(a, b, c);
        }
    })
})
$(function() {
    $(document).on("click", ".delete", function() {
        var deleteTarget = $(this);
        layer.msg('确认删除这条信息？', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function(index) {
                layer.close(index);
                var thisId2 = deleteTarget.parents('.listContent_item').find('.change').eq(0).text();
                $.ajax({
                    url: "http://" + apihost + "/v1/tproxy/" + thisId2,
                    type: 'delete',
                    dataType: 'json',
                    contentType: "application/json",
                    success: function(data) {
                        if (data.retcode == 0) {
                            deleteTarget.parents('.listContent_item').remove();
                            layer.msg('删除成功', { icon: 1 });
                        } else {
                            alert(data.desc)
                        }

                    },
                    error: function(a, b, c) {
                        alert(a, b, c);
                    }
                })
            }
        });

    });
});

/*tproxy信息添加*/
$(function() {
    $('.tproxyAdd').click(tproxyAdd);
});

/*tproxy信息添加函数*/
function tproxyAdd() {
    var machineName = $('.machineName').val();
    var machineCapacity = $('.machineCapacity').val();
    var machineRemarks = $('.machineRemarks').val();
    var machineIp = $('.machineIp').val();
    var userMode = $('.userMode').val();
    var startUsing = $('.startUsing').val();
    var startUsing2;

    if (machineName == '' || machineName == null || machineName == undefined) {
        layer.msg('请输入机器名', { icon: 2 });
        return
    }
    if (machineCapacity == '' || machineCapacity == null || machineCapacity == undefined) {
        layer.msg('请输入机器容量', { icon: 2 });
        return
    }
    if (machineRemarks == '' || machineRemarks == null || machineRemarks == undefined) {
        layer.msg('请输入备注', { icon: 2 });
        return
    }
    if (machineIp == '' || machineIp == null || machineIp == undefined) {
        layer.msg('请输入机器ip', { icon: 2 });
        return
    }
    if (startUsing == '' || startUsing == null || startUsing == 'undefined') {
        layer.msg('请选择是否启用', { icon: 2 });
        return
    }
    var html = '';
    (startUsing == 0) ? startUsing2 = "否": startUsing2 = "是";
    var postdata = {
        "Addr": machineIp,
        "Name": machineName,
        "Capacity": parseInt(machineCapacity),
        "Enable": parseInt(startUsing),
        "Remark": machineRemarks,
        "Unavailable": 1
    }
    $.ajax({
        url: "http://" + apihost + "/v1/tproxy/",
        type: 'post',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postdata),
        success: function(data) {
            if (data.retcode != 0) {
                alert(data.desc)
                return
            }
            layer.msg('添加成功', { icon: 1 });
            location.reload(true);

        },
        error: function(a, b, c) {
            alert(a, b, c);
        }
    })
}

/*tproxy信息修改*/
$(function() {
    $(document).on("click", ".amend", function() {
        var parent = $(this).parents('.listContent_item');
        layer.msg('请在添加信息处修改信息', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function(index) {
                layer.close(index);
                $('.tproxyAdd').css('display', 'none');
                $('.tproxyChange').css('display', 'inline-block');
                $('.machineName').val(parent.find('.change').eq(1).text());
                $('.machineCapacity').val(parent.find('.change').eq(3).text());
                $('.machineRemarks').val(parent.find('.change').eq(6).text());
                $('.machineIp').val(parent.find('.change').eq(2).text());
                $('.userMode').val(parent.find('.change').eq(4).text());
                $('.startUsing').val(0);
                $('.tproxyChange').click(function() {
                    changeFun(parent);
                });
            }
        });
    });
});

/*修改点击函数*/
function changeFun(parent) {
    var machineName = $('.machineName').val();
    var machineCapacity = $('.machineCapacity').val();
    var machineRemarks = $('.machineRemarks').val();
    var machineIp = $('.machineIp').val();
    var userMode = $('.userMode').val();
    var startUsing = $('.startUsing').val();
    var startUsing1;
    if (machineName == '' || machineName == null || machineName == 'undefined') {
        layer.msg('请输入机器名', { icon: 2 });
        return
    }
    if (machineCapacity == '' || machineCapacity == null || machineCapacity == 'undefined') {
        layer.msg('请输入机器容量', { icon: 2 });
        return;
    }
    if (machineRemarks == '' || machineRemarks == null || machineRemarks == 'undefined') {
        layer.msg('请输入备注', { icon: 2 });
        return
    }
    if (machineIp == '' || machineIp == null || machineIp == 'undefined') {
        layer.msg('请输入机器ip', { icon: 2 });
        return
    }
    if (startUsing == '' || startUsing == null || startUsing == 'undefined') {
        layer.msg('请选择是否启用', { icon: 2 });
        return
    }
    (startUsing == 0) ? startUsing1 = "否": startUsing1 = "是";
    parent.find('.change').eq(1).text(machineName);
    parent.find('.change').eq(2).text(machineIp);
    parent.find('.change').eq(3).text(machineCapacity);
    parent.find('.change').eq(4).text(startUsing1);
    parent.find('.change').eq(5).text(userMode);
    parent.find('.change').eq(6).text(machineRemarks);
    $('.tproxyAdd').css('display', 'inline-block');
    $('.tproxyChange').css('display', 'none');

    var postdata2 = { "Addr": machineIp, "Name": machineName, "Capacity": parseInt(machineCapacity), "Enable": parseInt(startUsing), "Remark": machineRemarks, "Unavailable": 1 }
    var thisId = parent.find('.change').eq(0).text();
    $.ajax({
        url: "http://" + apihost + "/v1/tproxy/" + thisId,
        type: 'put',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postdata2),
        success: function(data) {
            if (data.retcode == 0) {
                layer.msg('信息修改成功', { icon: 1 });
                location.reload(true);
            } else {
                alert(data.desc)
            }

        },
        error: function(a, b, c) {
            alert(a, b, c);
        }
    })
}